﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using KSSModel;

namespace KSSOnline.Controller
{
    public class CashHelper
    {
        public static List<tblPayCash> GetCashByUser(string Username)
        {
            var serializer = new JavaScriptSerializer();
            try
            {
                using (var dataContext = new KSSDBDataContext())
                {

                    var userCash = (from cash in dataContext.tblPayCashes
                                   where cash.UserName.Equals(Username)
                                        select  cash).ToList();
                    //var aView = serializer.Serialize(userCash);
                    return userCash;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static string GetUserCash()
        {
            var serializer = new JavaScriptSerializer();
            try
            {
                using (var dataContext = new KSSDBDataContext())
                {
                    
                    var userCash = from cash in dataContext.tblPayCashes
                                   group cash.PayCash by cash.UserName into g
                                   join user in dataContext.tblUsers on g.Key equals user.UserName
                                   join userdetail in dataContext.tblEDMSHRs on user.UserName equals userdetail.mstaffno
                                   select new {
                                    Username = user.UserName,
                                    Name = user.FirstName + " " + user.Lastname,
                                    Departmen= userdetail.sacnameTh, 
                                    Cash = g.Sum()
                                   };
                    var aView = serializer.Serialize(userCash);
                    return aView;
                }
            }
            catch (Exception ex)
            {
                return serializer.Serialize(new Result(false, ex.Message));
            }
        }
        public static List<UserCash> GetUserCashCsv()
        {
            var serializer = new JavaScriptSerializer();
            try
            {
                using (var dataContext = new KSSDBDataContext())
                {

                    var userCash = (from cash in dataContext.tblPayCashes
                                   
                                   join user in dataContext.tblUsers on cash.UserName equals user.UserName
                                   join userdetail in dataContext.tblEDMSHRs on user.UserName equals userdetail.mstaffno
                                    select new UserCash
                                   {
                                       UserName = user.UserName,
                                       Name = user.FirstName + " " + user.Lastname,
                                       Department = userdetail.sacnameTh,
                                       Cash = cash.PayCash,
                                       CashDate = cash.PayDate,
                                       DocID = cash.docID

                                   }).ToList();

                    return userCash;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static bool AddCash(tblPayCash cash)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                try
                {
                    dataContext.tblPayCashes.InsertOnSubmit(cash);
                    dataContext.SubmitChanges();
                    var log = new StringBuilder();
                    var doc = dataContext.tblDocs.Single(i => i.createdBy == cash.UserName);
                    if (doc != null)
                    {
                        doc.status = 40;
                    }
                    log.Append("User " + cash.UserName + " เอกสารได้รับอนุมัติเพิ่มเงิน " + cash.PayCash + " บาท");
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            log.ToString(),
                        username = "system",
                        logType = 0
                    });
                    return true;
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        logType = 1
                    });
                    return false;
                }

            }
        }
        public static int AddCashGroup(tblPayGroup paygroup){

             using (var dataContext = new KSSDBDataContext())
            {
                try
                {
                    dataContext.tblPayGroups.InsertOnSubmit(paygroup);
                    dataContext.SubmitChanges();
                    int id = paygroup.id;
                    return id;
                }
                 catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        logType = 1
                    });
                    return -1;
                }
             }
                   
            // You can retrieve the id from the object
            
        }

        public static bool AddCash(List<tblPayCash> cash)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                try
                {
                    dataContext.tblPayCashes.InsertAllOnSubmit(cash);
                    foreach (var item in cash)
	                {
                        var doc = dataContext.tblDocs.Single(i => i.id == item.docID);
                        if (doc  != null )
                        {
                            doc.status = 40;
                        }
	                }
                    dataContext.SubmitChanges();
                    var log = new StringBuilder();

                    //log.Append("User " + cash.UserName + " เอกสารได้รับอนุมัติเพิ่มเงิน " + cash.PayCash + " บาท");
                    //KSSMaster.AddLog(new tblLog()
                    //{
                    //    details =
                    //        log.ToString(),
                    //    username = "system",
                    //    logType = 0
                    //});
                    return true;
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        logType = 1
                    });
                    return false;
                }

            }
        }
        public static string printCash(List<tblPayCash> cash)
        {
            var serializer = new JavaScriptSerializer();
            var edms = new EDMSDBDataContext();
            using (var dataContext = new KSSDBDataContext())
            {
                try
                {
                    var kk = from c in cash
                             join d in dataContext.tblDocs
                             on c.docID equals d.id
                             select d;
                    var document = kk.ToList();
                    var doccomp = from d in document
                                  join u in dataContext.tblEDMSHRs
                                  on d.createdBy equals u.mstaffno
                                  select new
                                  {
                                      d.id,
                                      d.title,
                                      d.score,
                                      d.cash,
                                      u.mstaffno,
                                      u.mtitleTh,
                                      u.mnameTh,
                                      u.surnameTh,
                                      u.mlocation,
                                      u.sacnameTh
                                  };
                    return serializer.Serialize(doccomp);

                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            
        }
    }
}
